﻿@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Demo</title>
    <!-- 请勿在项目正式环境中引用该 layui.css 地址 -->
    <link href="~/libs/layui-v2.5.6/layui/css/layui.css" rel="stylesheet" />
    <script src="~/libs/jquery/jquery.js"></script>
</head>
<body>
    <form class="layui-form layui-row layui-col-space4">
        <div class="layui-col-md4">
            <div class="layui-input-wrap">
                <input type="text" name="meaCode" id="meaCode" placeholder="请输入单位编码" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-col-md4">
            <div class="layui-input-wrap">
                <input type="text" name="meaName" id="meaName" placeholder="请输入单位名称" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-btn-container layui-col-md4">
            <button class="layui-btn layui-btn-sm" lay-submit lay-filter="demo-table-search">搜索</button>
            <button type="reset" class="layui-btn layui-btn-sm layui-btn-primary">重置</button>
        </div>
    </form>

    <table class="layui-hide" id="demo" lay-filter="test">
    </table>
    <!-- 请勿在项目正式环境中引用该 layui.js 地址 -->
    <script src="~/libs/layui-v2.5.6/layui/layui.js"></script>
    <script type="text/html" id="toolbarDemo">
        <div class="layui-btn-container">
        <button type="button" class="layui-btn layui-btn-sm layui-btn-primary" lay-on="test-page-custom">
            添加
        </button>
        <button type="button" class="layui-btn layui-btn-sm layui-btn-warming" lay-event="updatemea">
            修改
        </button>
        <button class="layui-btn layui-btn-sm layui-btn-danger" lay-submit id="btnDelete" lay-event="deletes">删除</button>
          </div>
    </script>
    <script type="text/html" id="toolDemo">
        <div class="layui-clear-space">
          <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="detail">修改</a>
          <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delmea">删除</a>
        </div>
    </script>
    <script>
        layui.use(['table', 'form', 'layer', 'dropdown'], function () {
            var table = layui.table;
            var form = layui.form;
            var dropdown = layui.dropdown;
            var layer = layui.layer
            var datas = [];
            // 创建表格实例
            table.render({
                elem: '#demo',
                url: '/XiangHe/GetMeasureList',
                cols: [[
                    { type: 'checkbox', fixed: 'left' },
                    { field: 'measureCode', title: '单位编码', width: 150, sort: true },
                    { field: 'measureName', title: '单位名称', width: 150 },
                    { field: 'primaryFlag', title: '是否是主单位', width: 150 },
                    { field: 'enableFlag', title: '是否启用', width: 150 },
                    { field: 'remark', title: '备注', width: 150 },
                    { fixed: 'right', title: '操作', width: 200, minWidth: 125, templet: '#toolDemo' }
                ]],
                page: {
                    limit: 10,
                    limits: [10, 20, 30, 40, 50],
                    prev: '上一页',
                    next: '下一页',
                    first: '首页',
                    last: '尾页'
                },
                request: {
                    pageName: 'pageIndex',
                    limitName: 'pageSize'
                },
                parseData: function (res) {
                    console.log(res)
                    return {
                        "code": res.code, //解析接口状态
                        "count": res.count, //解析数据长度
                        "data": res.data//解析数据列表
                    };
                },
                height: 525,
                toolbar: '#toolbarDemo',
                done: function () {
                    var id = this.id;
                    //复选框选中事件
                    table.on('checkbox(test)', function (obj) {
                        datas.push(obj.data);
                    });
                }
            });

            // 搜索提交
            form.on('submit(demo-table-search)', function (data) {
                var field = data.field; // 获得表单字段
                // 执行搜索重载
                table.reload('demo', {
                    page: {
                        curr: 1 // 重新从第 1 页开始
                    },
                    where: field // 搜索的字段
                });
                layer.msg('搜索成功');
                return false; // 阻止默认 form 跳转
            });

            // 工具栏事件 删除、修改
            table.on('toolbar(test)', function (obj) {
                var id = obj.config.id;
                var checkStatus = table.checkStatus(id);
                var othis = lay(this);
                switch (obj.event) {
                    case 'deletes':
                        var data = checkStatus.data;
                        if (data.length == 0) {
                            layer.msg('请选择一行');
                            return;
                        }
                        var ids = "";
                        for (var i = 0;i<data.length;i++){
                            ids += data[i].id + ",";
                        }
                        console.log(ids)
                        //获取多个id
                        // var ids = data.map(m => m.id);
                        layer.confirm("确定删除吗？", function (index) {
                            $.ajax({
                                url: "/XiangHe/DeleteRangeMeasure",
                                type: "get",
                                async: true,
                                data: { id: ids },
                                success: res => {
                                    if (res == 1 ) {
                                        layer.msg('删除成功');
                                        // 刷新页面
                                        table.reload('demo', {
                                            page: {
                                                curr: 1 // 重新从第 1 页开始
                                            }
                                        });
                                        return;
                                    } else {
                                        layer.msg('删除失败');
                                    }
                                }
                            });
                        });
                        break;
                    //修改
                    case 'updatemea':
                        var data = checkStatus.data;
                        if (data.length == 0) {
                            layer.msg('请选择一行');
                            return;
                        }
                        if (data.length > 1) {
                            layer.msg('只能修改一条');
                            return;
                        }
                        layer.open({
                            type: 2,
                            area: ['600px', '500px'],
                            resize: false,
                            shadeClose: true,
                            title: '修改单位',
                            content: `/XiangHe/MesuresUpdate?id=` + data[0].id,
                            btn: ['确定', '取消'],
                            yes: function (index, layero) {
                                // 获取弹出层中的表单数据
                                var data = layer.getChildFrame('body', index).find('form').serialize();
                                console.log(data)
                                // 发送Ajax请求
                                $.ajax({
                                    url: "/XiangHe/UpdateMeasure",
                                    type: "get",
                                    async: true,
                                    data: data,
                                    contentType: "application/x-www-form-urlencoded",
                                    success: res => {
                                        if (res != null) {
                                            alert("修改成功")
                                            //关闭弹出层
                                            layer.closeAll();
                                            //刷新表格
                                            table.reload('demo', {
                                                page: {
                                                    curr: 1 // 重新从第 1 页开始
                                                }
                                            });
                                            return;
                                        }
                                        else {
                                            console.log(res.code)
                                            alert("修改失败")
                                        }
                                    }
                                });
                                return false; // 阻止默认 form 跳转
                            },
                        });
                        break;
                };
            });

            // 触发单元格工具事件
            table.on('tool(test)', function (obj) { // 双击 toolDouble
                var data = obj.data; // 获得当前行数据
                // console.log(obj)
                if (obj.event === 'detail') {
                    layer.open({
                        type: 2,
                        area: ['600px', '500px'],
                        resize: false,
                        shadeClose: true,
                        title: '修改单位',
                        content: `/XiangHe/MesuresUpdate?id=`+data.id,
                        btn: ['确定', '取消'],
                        yes: function (index, layero) {
                            // 获取弹出层中的表单数据
                            var data = layer.getChildFrame('body', index).find('form').serialize();
                            // 发送Ajax请求
                            $.ajax({
                                url: "/XiangHe/UpdateMeasure",
                                type: "get",
                                async: true,
                                data: data,
                                contentType: "application/x-www-form-urlencoded",
                                success: res => {
                                    if (res != null) {
                                        alert("修改成功")
                                        //关闭弹出层
                                        layer.closeAll();
                                        //刷新表格
                                        table.reload('demo', {
                                            page: {
                                                curr: 1 // 重新从第 1 页开始
                                            }
                                        });
                                        return;
                                    }
                                    else {
                                        console.log(res.code)
                                        alert("修改失败")
                                    }
                                }
                            });
                            return false; // 阻止默认 form 跳转
                        },
                    });
                }
                else if (obj.event = "delmea") {
                    layer.confirm("确定删除吗？", function (index) {
                        $.ajax({
                            url: "/XiangHe/DeleteMeasure",
                            type: "get",
                            async: true,
                            data: { id: data.id },
                            success: res => {
                                if (res != null) {
                                    layer.msg('删除成功');
                                    // 刷新页面
                                    table.reload('demo', {
                                        page: {
                                            curr: 1 // 重新从第 1 页开始
                                        }
                                    });
                                    return;
                                } else {
                                    layer.msg('删除失败');
                                }
                            }
                        });
                    });
                }
            })
        });
    </script>

    @*添加*@
    <script>
        layui.use(function () {
            var $ = layui.$;
            var layer = layui.layer;
            var table = layui.table;
            var util = layui.util;
            var form = layui.form;
            // 事件
            util.on('lay-on', {
                'test-page-custom': function () {
                    layer.open({
                        type: 2,
                        area: ['800px', '500px'],
                        resize: false,
                        shadeClose: true,
                        title: '添加单位',
                        content: `/XiangHe/MesuresAdd`,
                        btn: ['确定', '取消'],
                        yes: function (index, layero) {
                            // 获取弹出层中的表单数据
                            var data = layer.getChildFrame('body', index).find('form').serialize();
                            // 发送Ajax请求
                            $.ajax({
                                url: "/XiangHe/AddMeasure",
                                type: "get",
                                async: true,
                                data: data,
                                contentType: "application/x-www-form-urlencoded",
                                success: res => {
                                    console.log(res)
                                    if (res == 1) {
                                        alert("添加成功")
                                        //关闭弹出层
                                        layer.closeAll();
                                        //刷新表格
                                        table.reload('demo', {
                                            page: {
                                                curr: 1 // 重新从第 1 页开始
                                            }
                                        });
                                        return;
                                    }
                                    else if (res == -1) {
                                        alert("不可重复添加")
                                    }
                                    else {
                                        alert("添加失败")
                                    }
                                }
                            });
                            return false; // 阻止默认 form 跳转
                        },
                    });
                }
            })
        });
    </script>
</body>
</html>